package com.app.base.tool;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.List;

/**
 * 时间:2015-7-6
 * 公司:DDY
 * 作者:LBH
 * 功能:适配器基类
 * @author Administrator
 * @param <T>模型类型
 * @param <Q>空间类型
 *
 */
public abstract class BaseAdapter<T, Q>  extends android.widget.BaseAdapter{
	
	protected Context context;
	protected List<T> models;
	protected Q view; //这里不一定是ListView,比如GridView
	protected DisplayImageOptions options; //图像处理  也可以不使用这个

	public BaseAdapter(Context context,List<T> models,DisplayImageOptions options){
		this.context = context;
		this.models = models;
		this.options = options;
	}

	public BaseAdapter(Context context,List<T> models,Q view){
		this.context = context;
		this.models = models;
		this.view = view;
	}

	
	public BaseAdapter(Context context,List<T> models,Q view,DisplayImageOptions options){
		this.context = context;
		this.models = models;
		this.view = view;
		this.options = options;
	}
	
	

	@Override
	public int getCount() {
		return models.size();
	}


	@Override
	public Object getItem(int position) {
		return models.get(position);
	}


	@Override
	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * 下拉刷新 增加数据
	 * @param updates
	 */
	public void setUpdate(List<T> updates){
		if(isRequestList(updates)){
			models.addAll(updates);
			this.notifyDataSetChanged();
		}
	};
	
	public <T> boolean isRequestList(List<T> requestList) {
		return (requestList != null && !requestList.isEmpty()) ? true : false;
	}
	
	/**
	 * 判断(字符窜)返回值是否存在
	 * @param requestStr
	 * @return
	 */
	public boolean isRequestStr(String requestStr){
		return (requestStr!=null && requestStr.length()>0 && !requestStr.equals("[]"))?true:false;
	}
	/**
	 * 判断(LONG)返回值是否存在
	 * @return
	 */
	public boolean isRequestLong(Long requestLong){
		return (requestLong != null && requestLong >0 )?true:false;
	}
	
	/**
	 * 获取的属性 为空转化为""
	 * @param attribute
	 * @return
	 */
	public String setAttributeText(Object attribute){
		String str = (attribute == null) ?"":attribute.toString();
		return str;
	}
	
	/**
	 * 设置界面文字展示 统一使用html封装
	 * @param textView
	 * @param content
	 */
	public void textViewSetText(TextView textView, String content){
		if(isRequestStr(content)){
			Spanned fromHtml = Html.fromHtml(content);
			textView.setText(fromHtml);
		}else{
			textView.setText(setAttributeText(content));
		}
	}
}
